package com.qk.service.impl;

import com.qk.mapper.BusinessMapper;
import com.qk.mapper.ClueMapper;
import com.qk.service.ReportService;
import com.qk.vo.OverviewVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

@Slf4j
@Service
public class ReportServiceImpl implements ReportService {
@Autowired
private ClueMapper clueMapper;
@Autowired
private BusinessMapper businessMapper;
@Autowired
private RedisTemplate<Object, Object> redisTemplate;

    @Override
    public OverviewVO getOverview() {
        //1、查询redis缓存中的数据
        Object dataOverview = redisTemplate.opsForValue().get("DATA_OVERVIEW");
        //1.1、判断缓存数据是否存在
        if (dataOverview != null) {
            log.info("查询redis缓存中的数据：{}", dataOverview);
            return (OverviewVO) dataOverview;
        }
        //2、查询数据库
        //2.1、获取线索概览数据
        OverviewVO clueOverviewVO = clueMapper.getClueOverviewData();
        //2.2、获取商机概览数据
        OverviewVO businessOverviewVO = businessMapper.getBusinessOverviewData();
        //2.3、获取合同概览数据
        BeanUtils.copyProperties(businessOverviewVO, clueOverviewVO, "clueTotal", "clueWaitAllot", "clueWaitFollow", "clueFollowing", "clueFalse", "clueConvertBusiness");
        //3、缓存数据到redis中
        redisTemplate.opsForValue().set("DATA_OVERVIEW", clueOverviewVO,1, TimeUnit.MINUTES);
        return clueOverviewVO;
    }
}
